<?php

namespace app\common\model;

use think\facade\Cache;
use think\Model;

class PropsNewModel extends Model
{
    // 道具类型
    const TYPE_CATE = [
        100 => '喇叭',
        101 => '弹幕',
        102 => '坐骑',
        // 103 => '改名卡',
        // 104 => '体验卡',
        105 => '观赛券',
    ];

    public static function getTypeTextAttr($value, $data)
    {
        return isset(self::TYPE_CATE[$data['type']]) ? self::TYPE_CATE[$data['type']] : '';
    }

    protected static function init()
    {
        static::afterUpdate(function ($user) {
            Cache::rm('props_list');
        });
        static::afterInsert(function ($user) {
            Cache::rm('props_list');
        });
    }

    public static function lists(): array
    {
        return Cache::remember('props_list', function () {
            return array_column(static::select()->toArray(), null, 'id');
        });
    }

    public static function listByType(int $type = 102): array
    {
        $list = [];
        foreach (static::lists() as $item) {
            if ($item['type'] === $type) {
                $list[] = $item;
            }
        }

        return $list;
    }
}